package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.Route;

public class RouteDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	private String TAG="swadeshi";
	
	
	public RouteDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	/*
	 * insert code
	 */
	@SuppressWarnings("static-access")
	public int createOrUpdateFRoute(ArrayList<Route>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
    for (Route route : list) {
    		ContentValues values = new ContentValues();
			
			values.put(dbHelper.FROUTE_REPCODE, route.getFROUTE_REPCODE());
			values.put(dbHelper.FROUTE_ROUTECODE, route.getFROUTE_ROUTECODE());
			values.put(dbHelper.FROUTE_ROUTE_NAME, route.getFROUTE_ROUTE_NAME());
			values.put(dbHelper.FROUTE_RECORDID, route.getFROUTE_RECORDID());
			values.put(dbHelper.FROUTE_ADDDATE, route.getFROUTE_ADDDATE());
			values.put(dbHelper.FROUTE_ADD_MACH, route.getFROUTE_ADD_MACH());
			values.put(dbHelper.FROUTE_ADD_USER, route.getFROUTE_ADD_USER());
			values.put(dbHelper.FROUTE_AREACODE, route.getFROUTE_AREACODE());
			values.put(dbHelper.FROUTE_DEALCODE, route.getFROUTE_DEALCODE());
			values.put(dbHelper.FROUTE_FREQNO, route.getFROUTE_FREQNO());
			values.put(dbHelper.FROUTE_KM, route.getFROUTE_KM());
			values.put(dbHelper.FROUTE_MINPROCALL, route.getFROUTE_MINPROCALL());
			values.put(dbHelper.FROUTE_RDALORATE, route.getFROUTE_RDALORATE());
			values.put(dbHelper.FROUTE_RDTARGET, route.getFROUTE_RDTARGET());
			values.put(dbHelper.FROUTE_REMARKS, route.getFROUTE_REMARKS());
			values.put(dbHelper.FROUTE_STATUS, route.getFROUTE_STATUS());
			values.put(dbHelper.FROUTE_TONNAGE, route.getFROUTE_TONNAGE());
			
			count = (int) dB.insert(dbHelper.TABLE_FROUTE, null, values);

			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	/*
	 * delete code
	 */
	@SuppressWarnings("static-access")
	public int deleteAll(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FROUTE, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FROUTE, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	@SuppressWarnings("static-access")
	public ArrayList<String> getAllRouteByRepCode(String repCode) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> list = new ArrayList<String>();
		
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FROUTE +" WHERE "+dbHelper.FROUTE_REPCODE+"='"+repCode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			list.add(cursor.getString(cursor.getColumnIndex(dbHelper.FROUTE_ROUTE_NAME)));
			
		}
		
		return list;
	}
	
	@SuppressWarnings("static-access")
	public String getRouteCodeByRoute(String route) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
				
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FROUTE +" WHERE "+dbHelper.FROUTE_ROUTE_NAME+"='"+route+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FROUTE_ROUTECODE));
			
		}
		
		return "";
	}
	
	
	@SuppressWarnings("static-access")
	public String getRouteNameByCode(String code) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
				
		String selectQuery = "SELECT * FROM "+dbHelper.TABLE_FROUTE+" R, "+dbHelper.TABLE_FROUTEDET+" RD "
				+ "WHERE R."+dbHelper.FROUTE_ROUTECODE+"=RD."+dbHelper.FROUTEDET_ROUTE_CODE
						+ " AND RD."+dbHelper.FROUTEDET_DEB_CODE+"='"+code+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FROUTE_ROUTE_NAME));
			
		}
		
		return "";
	}
	
	
	@SuppressWarnings("static-access")
	public String getRouteNameByRouteCode(String rCode) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
				
		String selectQuery = "SELECT * FROM "+dbHelper.TABLE_FROUTE+" R, "+dbHelper.TABLE_FROUTEDET+" RD "
							+ "WHERE R."+dbHelper.FROUTE_ROUTECODE+"=RD."+dbHelper.FROUTEDET_ROUTE_CODE
							+ " AND RD."+dbHelper.FROUTE_ROUTECODE+"='"+rCode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FROUTE_ROUTE_NAME));
			
		}
		
		return "";
	}
}
